<?php
namespace app\super\controller;
use think\Db;
use app\super\validate\TeamValidate;
class TeamController extends CommonController {

    public function getTeamInfo(){
        $uid = session('loginSession');
        $where='';
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['team_name|team_number|uuu.user_username'] = array('like',"%$keyword%");
        }

        if($_POST['start']!=''){//时间
            $where['team_time'] =array('>=',$_POST['start']);
        }elseif ($_POST['end']!=''){
            $where['team_time'] =array('<=',$_POST['end']);
        }elseif ($_POST['start']!=''&&$_POST['end']!=''){
            $where['team_time'] =array('between',array($_POST['start'],$_POST['end']));

        }
//        if($_POST['end']!=''){//时间
//            $where['team_time'] =array('<=',$_POST['end']);
//        }
        if($_POST['team_status']!=''){
            $where['team_status'] = $_POST['team_status'];
        }
        $res = Db::table('up_team')->alias('a')
            ->join('up_u_user uuu','uuu.user_id=a.user_id','left')
            ->join('up_u_admin uua','uua.user_id = a.examine_user_id','left')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->field('a.*,uuu.user_username,uua.user_username as username')
            ->order('team_status ASC')
            ->select();
        $total = Db::table('up_team')->alias('a')
            ->join('up_u_user uuu','uuu.user_id=a.user_id','left')
            ->join('up_u_admin uua','uua.user_id = a.examine_user_id','left')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->count();
        foreach ($res as $key=>&$vo){
            $whereMember['team_id'] = $vo['team_id'];
            $whereMember['team_member_remove'] = 2;
            $member_count = Db::table('up_team_members')->where($whereMember)->count();//人员数量
            $whereRelation['team_id'] =$vo['team_id'];
            $whereRelation['tr_status'] =2;
            $Relation_count = Db::table('up_team_relation')->where($whereRelation)->count();//管理团队数量
            $whereAdv['team_id'] =$vo['team_id'];
            $Adv_count = Db::table('up_team_adv')->where($whereAdv)->count();//公告数量
            $whereAssignment['team_id'] = $vo['team_id'];
            $Assignment_count = Db::table('up_team_assignment')->where($whereAdv)->count();//任务数量
//            if($uid['user_type']==1){
//                $vo['is_sh'] = 1;//审核权限
//            }
            $vo['member_count'] = $member_count;
            $vo['Relation_count'] = $Relation_count;
            $vo['Adv_count'] = $Adv_count;
            $vo['Assignment_count'] = $Assignment_count;
        }
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /***
     * 停用
     */
    public function Confirmation(){
        $cid = $_POST['cid'];
        $res = Db::table('up_team')->where('team_id',$cid)->setField('status',2);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "操作成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "操作失败";
            return json_encode($returnData);
        }
    }
    /***
     * 启用
     */
    public function QyConfirmation(){
        $cid = $_POST['cid'];
        $res = Db::table('up_team')->where('team_id',$cid)->setField('status',1);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "操作成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "操作失败";
            return json_encode($returnData);
        }
    }

    /**
     * 添加团队页面
     */
    public function addTeam(){
        $res = Db::table('up_u_user')->where('user_status',1)->field('user_id,user_username,user_phone')->select();
        $this->assign('res',$res);
        return $this->fetch('team/addTeam');

    }
    /***
     * 添加操作
     */
    public function insertTeam(){
        $validata = new TeamValidate();
        if(!$validata->check($_POST)){
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }
        if(strlen($_POST['team_number']) > 8){
            $returnData['status'] = 0;
            $returnData['msg'] = "团队ID不能超过8位";
            return json_encode($returnData);
        }
        //判断这个团队编号是否已经存在
        $team_id = Db::table('up_team')->where('team_number',$_POST['team_number'])->find()['team_id'];
        if($team_id){
            $returnData['status'] = 0;
            $returnData['msg'] = "团队ID已经存在,请重新填写";
            return json_encode($returnData);
        }
        if($_FILES['Img']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "企业营业执照不能为空";
            return json_encode($returnData);
        }else{
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['Img']['name']);
            $filePath = $_FILES['Img']['tmp_name'];
            $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
            if($info['status']==200) {
                $res['co_img'] = QiNiu.$info['key'];
            }
        }

        if($_FILES['fileImg']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "团队头像不能为空";
            return json_encode($returnData);
        }else{
            $rand_img = date('YmdHis').rand(100000,999999);
            $exe_img = explode('.',$_FILES['fileImg']['name']);
            $filePath_img = $_FILES['fileImg']['tmp_name'];
            $info_img = $this->qnCommon(2,$rand_img,$exe_img[1],$filePath_img);
            if($info_img['status']==200) {
                $data['team_img'] = QiNiu.$info_img['key'];
            }

        }
        Db::startTrans();//开启事物

        /**团队信息**/
        $data['team_id'] =$this->getRandChar(32);
        $data['team_name'] = $_POST['team_name'];
        $data['team_number'] = $_POST['team_number'];
        $data['team_explain'] = $_POST['team_explain'];
        $data['user_id'] = $_POST['user_id'];
        $data['team_time'] = date('Y-m-d H:i:s');
        $data['team_status'] =2;
//        $data['is_team'] = $_POST['is_team'];
        $return_res = Db::table('up_team')->insert($data);
        /*****团队认证信息*******/
         $res['co_name']  =$_POST['co_name'];
         $res['co_code']   =$_POST['co_code'];
         $res['co_address']=$_POST['co_address'];
         $res['co_people'] =$_POST['co_people'];
         $res['team_id']   =$data['team_id'];
         $res['co_commit_time'] = date('Y-m-d H:i:s');
         $res['ckt_id']    =$this->getRandChar(32);
        $return_result = Db::table('up_check_team')->insert($res);
        //决策层
        $data_role1['team_id'] = $data['team_id'];
        $data_role1['team_role'] = 1;
        $data_role1['team_role_detail'] = '1,2,3';
        $data_role1['team_role_id'] = $this->getRandChar(32);
        //管理层
        $data_role2['team_id'] = $data['team_id'];
        $data_role2['team_role'] = 2;
        $data_role2['team_role_detail'] = '2,3';
        $data_role2['team_role_id'] = $this->getRandChar(32);
        //成员层
        $data_role3['team_id'] = $data['team_id'];
        $data_role3['team_role'] = 3;
        $data_role3['team_role_detail'] = '13';
        $data_role3['team_role_id'] = $this->getRandChar(32);
        $res1 = Db::table('up_team_role')->insert($data_role1);
        $res2 = Db::table('up_team_role')->insert($data_role2);
        $res3 = Db::table('up_team_role')->insert($data_role3);
        if($return_res&&$return_result&&$res1&&$res2&&$res3){
            Db::commit();//提交事物

            $returnData['status'] = 1;
            $returnData['msg'] = "创建成功";
            return json_encode($returnData);
        }else{
            Db::rollback();//回滚

            $returnData['status'] = 0;
            $returnData['msg'] = "创建失败";
            return json_encode($returnData);
        }

    }

    /***
     * 审核页面
     */
    public function TeamInfo(){
        $cid =$_POST['cid'];
        $res = Db::table('up_check_team')->where('team_id',$cid)->find();
        $this->assign('res',$res);
        $this->assign('cid',$cid);
        return $this->fetch('team/TeamInfo');
    }
    /**
     * 审核功能
     */
    public function updateTeamInfo(){
        if($_POST['team_status']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "审核状态不能为空";
            return json_encode($returnData);
        }

        if($_POST['team_status']==3){
            if(trim($_POST['team_nopass_reason'])==''){
                $returnData['status'] = 0;
                $returnData['msg'] = "审核不通过原因不能为空";
                return json_encode($returnData);
            }else{
                $data['team_nopass_reason'] = $_POST['team_nopass_reason'];

            }
        }
        $uid = session('loginSession')['user_id'];

        $data['team_status'] = $_POST['team_status'];
        $data['examine_user_id'] =$uid;
        $res = Db::table('up_team')->where('team_id',$_POST['team_id'])->update($data);
        $TeamInfo = Db::table('up_team')->where('team_id',$_POST['team_id'])->find();
        if($res){
            if($_POST['team_status']==2){
                $msg1 = "{\"msg\":\"6\"}";
                $msg='您提交的'.$TeamInfo['team_name'].'资质认证通过,快去查看';
                $msg_data = array('msg'=>$msg,'code'=>6,'fid'=>$TeamInfo['team_id']);
                $code1 = 6;
                $code = 6;
                $this->sendJpush(1,$msg,$msg_data,$TeamInfo['user_id'],$msg1,$code,2,$code1);
            }else{
                $msg1 = "{\"msg\":\"5\"}";
                $msg='您提交的'.$TeamInfo['team_name'].'资质认证未通过,快去查看原因';
                $msg_data = array('msg'=>$msg,'code'=>5,'fid'=>$TeamInfo['team_id']);
                $code = 5;
                $code1 = 5;
                $this->sendJpush(1,$msg,$msg_data,$TeamInfo['user_id'],$msg1,$code,2,$code1);
            }
            $returnData['status'] = 1;
            $returnData['msg'] = "审核成功";
            return json_encode($returnData);

        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "审核失败";
            return json_encode($returnData);
        }
    }

    /**
     * 编辑页面
     */
    public function  updateTeamRes(){
        $nid = $_POST['nid'];
        $team = Db::table('up_team')->where('team_id',$nid)->find();
        $check_team = Db::table('up_check_team')->where('team_id',$nid)->find();
        $res = Db::table('up_u_user')->where('user_status',1)->field('user_id,user_username,user_phone')->select();
        $this->assign('res',$res);
        $this->assign('team',$team);
        $this->assign('check_team',$check_team);
        return $this->fetch('team/updateTeamRes');
    }

    /**
     * 编辑功能
     */
    public function insertTeamInfo(){
        $team_id_s = $_POST['team_id'];//团队主键
        $check_team_id = $_POST['check_team_id'];//团队审核信息主键
        $validata = new TeamValidate();
        if(!$validata->check($_POST)){
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }
        if(strlen($_POST['team_number']) > 8){
            $returnData['status'] = 0;
            $returnData['msg'] = "团队ID不能超过8位";
            return json_encode($returnData);
        }
        //判断这个团队编号是否已经存在
        $where['team_number'] = $_POST['team_number'];
        $where['team_id'] = array('NEQ',$team_id_s);
        $team_id = Db::table('up_team')->where($where)->find()['team_id'];
        if($team_id){
            $returnData['status'] = 0;
            $returnData['msg'] = "团队ID已经存在,请重新填写";
            return json_encode($returnData);
        }
        if($_FILES['fileImg']['name']!=''){//团队头像不为空
                $rand_img = date('YmdHis').rand(100000,999999);
                $exe_img = explode('.',$_FILES['fileImg']['name']);
                $filePath_img = $_FILES['fileImg']['tmp_name'];
                $info_img = $this->qnCommon(2,$rand_img,$exe_img[1],$filePath_img);
                if($info_img['status']==200) {
                    $data['team_img'] = QiNiu.$info_img['key'];
                }
        }
        if($_FILES['Img']['name']!=''){//企业营业执照
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['Img']['name']);
            $filePath = $_FILES['Img']['tmp_name'];
            $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
            if($info['status']==200) {
                $res['co_img'] = QiNiu.$info['key'];
                $data['team_status'] = 4;//如果企业营业执照修改，团队信息就会变成未认证状态
            }
        }
        Db::startTrans();//开启事物
        $result = Db::table('up_check_team')->where('ckt_id',$check_team_id)->find();
        if(($result['co_name']!=$_POST['co_name'])
            || ($result['co_code']!=$_POST['co_code'])
            ||($result['co_address']!=$_POST['co_address'])
            ||($result['co_people']!=$_POST['co_people']))
        {
            $data['team_status'] = 4;
        }
        /**团队信息**/
        $data['team_name'] = $_POST['team_name'];
        $data['team_number'] = $_POST['team_number'];
        $data['team_explain'] = $_POST['team_explain'];
        $data['user_id'] = $_POST['user_id'];
        $return_res = Db::table('up_team')->where('team_id',$team_id_s)->update($data);
        /*****团队认证信息*******/
        $res['co_name']  =$_POST['co_name'];
        $res['co_code']   =$_POST['co_code'];
        $res['co_address']=$_POST['co_address'];
        $res['co_people'] =$_POST['co_people'];
        $res['co_commit_time'] = date('Y-m-d H:i:s');
        $return_result = Db::table('up_check_team')->where('ckt_id',$check_team_id)->update($res);
        if($return_res&&$return_result){
            Db::commit();//提交事物

            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }else{
            Db::rollback();//回滚

            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }

    /**
     * @return mixed
     * 查看关联团队列表
     */
    public function CkTeam(){
        $cid = $_POST['cid'];
        $where['a.team_id'] =$cid;
        $where['a.tr_status'] =2;
        $res= Db::table('up_team_relation')->alias('a')
                        ->where($where)
                        ->join('up_team ut','ut.team_id = a.relation_team_id','left')
                        ->field('ut.team_id,ut.team_name,ut.team_number')
                        ->select();
        $this->assign('res',$res);
        return $this->fetch('team/CkTeam');
    }

    /***
     * 关联团队页面
     */
    public function relationTeam(){
        $nid =$_POST['nid'];
        $whereMap['team_id'] = $nid;
        $whereMap['tr_status'] =2;
        //查询出这个关联团队的信息
        $relation_team_id= Db::table('up_team_relation')->where($whereMap)->column('relation_team_id');

//        array_push($relation_team_id,$nid);
        if(count($relation_team_id)>0){
            $where['team_id'] =array('not in',$relation_team_id);//不属于这个关联团队的团队
        }
        $where['status'] = 1;
        $where['team_status'] = 2;
        $team = Db::table('up_team')->where($where)->select();
        $team_one = Db::table('up_team')->where('team_id',$nid)->find();//查询关联团队
        $this->assign('team',$team);
        $this->assign('team_one',$team_one);
        return $this->fetch('team/relationTeam');
    }

    /***
     * 添加关联团队功能
     */
    public function insertReTeam(){
        if(count($_POST['team'])<=0){
            $returnData['status'] = 0;
            $returnData['msg'] = "关联团队不能为空";
            return json_encode($returnData);
        }else{
            $data = array();
            foreach ($_POST['team'] as $key=>$vo){
                $res['team_id'] = $_POST['team_one'];
                $res['relation_team_id'] = $vo;
                $res['tr_status'] = 2;
                $res['tr_time'] = date('Y-m-d H:i:s');
                $res['tr_id'] = $this->getRandChar(32);
                $data[] = $res;
            }
           $result = Db::table('up_team_relation')->insertAll($data);
            if($result){
                array_push($_POST['team'],$_POST['team_one']);
                $where['team_id'] =array('IN',$_POST['team']);
                $user_Array = Db::table('up_team')->where($where)->column('user_id');
                $msg1 = "{\"msg\":\"11\"}";
                $msg='平台已经为您的团队,添加了关联团队,快去看看';
                $msg_data = array('msg'=>$msg,'code'=>11,'fid'=>'');
                $code = 11;
                $code1 = 11;
                $this->sendJpush(1,$msg,$msg_data,$user_Array,$msg1,$code,1,$code1);
                $returnData['status'] = 1;
                $returnData['msg'] = "关联团队成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "关联团队失败";
                return json_encode($returnData);
            }
        }
    }

    /***
     * 解除关联团队
     */
    public function relieveTeam(){
        $nid =$_POST['nid'];
        $whereMap['team_id'] = $nid;
        $whereMap['tr_status'] = 2;
        //查询出这个关联团队的信息
        $relation_team_id= Db::table('up_team_relation')->where($whereMap)->column('relation_team_id');
        $where['team_id'] =array('NEQ',$nid);//不属于这个关联团队的团队
        $where['status'] = 1;
        $where['team_status'] = 2;
        $team = Db::table('up_team')->where($where)->select();
        $team_one = Db::table('up_team')->where('team_id',$nid)->find();//查询关联团队
        $this->assign('team',$team);//除去自己的所有团队
        $this->assign('team_one',$team_one);
        $this->assign('data',$relation_team_id);//自己关联的团队
        return $this->fetch('team/relieveTeam');
    }

    /**
     * @return string
     * 解除团队功能
     */
    public function insertRelieveTeam(){
        if(!isset($_POST['team'])){
            $returnData['status'] = 0;
            $returnData['msg'] = "关联团队不能为空";
            return json_encode($returnData);
        }
        if(count($_POST['team'])<=0){
            $returnData['status'] = 0;
            $returnData['msg'] = "关联团队不能为空";
            return json_encode($returnData);
        }else{
            Db::table('up_team_relation')->where('team_id',$_POST['team_one'])->delete();
            $data = array();
            foreach ($_POST['team'] as $key=>$vo){
                $res['team_id'] = $_POST['team_one'];
                $res['relation_team_id'] = $vo;
                $res['tr_status'] = 2;
                $res['tr_time'] = date('Y-m-d H:i:s');
                $res['tr_id'] = $this->getRandChar(32);
                $data[] = $res;
            }
            $result = Db::table('up_team_relation')->insertAll($data);
            if($result){
                $returnData['status'] = 1;
                $returnData['msg'] = "关联团队成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "关联团队失败";
                return json_encode($returnData);
            }
        }
    }

    /**
     * 添加团队成员页面
     */
    public function addTeamMember(){
        $nid =$_POST['nid'];
        $team_one = Db::table('up_team')->where('team_id',$nid)->find();//查询关联团队信息
        //这个团队所有的成员
        $where['team_id'] = $nid;
        $where['team_member_remove'] = 2;

        $team_member_user_id = Db::table('up_team_members')->where($where)->column('team_member_user_id');
        //查询出不属于这个团队的所有用户
        if(count($team_member_user_id)>0){
            $whereU['user_id'] = array('not in',$team_member_user_id);
        }
        $whereU['user_status'] = 1;
        $userInfo = Db::table('up_u_user')->where($whereU)->field('user_id,user_username,user_phone')->select();
        $this->assign('userInfo',$userInfo);
        $this->assign('team_one',$team_one);
        return $this->fetch('team/addTeamMember');
    }

    /***
     * 添加团队成员
     */
    public function insertTeamMember(){
        //判断权限是否存在交集
        $intersection1='';
        $intersection2='';
        $intersection3='';
        if(isset($_POST['teamRole1'])&&isset($_POST['teamRole2'])){
            $intersection1 = array_intersect($_POST['teamRole1'],$_POST['teamRole2']);
        }
        if(isset($_POST['teamRole1'])&&isset($_POST['teamRole3'])){
            $intersection2 = array_intersect($_POST['teamRole1'],$_POST['teamRole3']);
        }
        if(isset($_POST['teamRole2'])&&isset($_POST['teamRole3'])){
            $intersection3 = array_intersect($_POST['teamRole2'],$_POST['teamRole3']);
        }

        if($intersection1||$intersection2||$intersection3){
            $returnData['status'] = 0;
            $returnData['msg'] = "添加团队成员不能出现一个人多个角色";
            return json_encode($returnData);
        }
        $team_id = $_POST['team_one'];//团队ID
        $team_list = Db::table('up_team')->where('team_id',$team_id)->field('team_name')->find();
        if(isset($_POST['teamRole1'])===true){
            if(count($_POST['teamRole1'])>0){//团队决策层
                foreach ($_POST['teamRole1'] as $key=>$vo){
                    $where1['team_id'] = $team_id;
                    $where1['team_member_user_id'] =$vo;
                    $role1 = Db::table('up_team_members')->where($where1)->find();
                    if($role1['team_member_id']==''){//不存在直接插入
                        $data1['team_member_id'] = $this->getRandChar(32);
                        $data1['team_id'] = $team_id;
                        $data1['team_member_user_id'] = $vo;
                        $data1['team_member_role'] = 1;
                        $data1['team_member_join_time'] = date('Y-m-d H:i:s');
                        Db::table('up_team_members')->where($where1)->insert($data1);

                    }else{//存在的话
                        if($role1['team_member_remove']==1){//先判断这个人已经被移出
                            $data2['team_member_remove'] = 2;
                            if($role1['team_member_role']!=1){
                                $data2['team_member_role'] =1;
                            }
                            $where2['team_member_id'] = $role1['team_member_id'];
                            Db::table('up_team_members')->where($where2)->update($data2);
                        }else{//没有被移出
                            if($role1['team_member_role']!=1){
                                $data3['team_member_role'] =1;
                                $where3['team_member_id'] = $role1['team_member_id'];
                                Db::table('up_team_members')->where($where3)->update($data3);
                            }
                        }

                    }
                }
                $msg1 = "{\"msg\":\"15\"}";
                $msg = '平台管理员把您设为'.$team_list['team_name'].'团队管理的决策层';
                $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                $code = 15;
                $code1 = 15;
                $this->sendJpush(1,$msg,$msg_data,$_POST['teamRole1'],$msg1,$code,1,$code1);
            }

        }
        if(isset($_POST['teamRole2'])===true){
            if(count($_POST['teamRole2'])>0){//团队管理层
                foreach ($_POST['teamRole2'] as $k=>$v){
                    $where4['team_id'] = $team_id;
                    $where4['team_member_user_id'] =$v;
                    $role1 = Db::table('up_team_members')->where($where4)->find();
                    if($role1['team_member_id']==''){//不存在直接插入
                        $data4['team_member_id'] = $this->getRandChar(32);
                        $data4['team_id'] = $team_id;
                        $data4['team_member_user_id'] = $v;

                        $data4['team_member_role'] = 2;
                        $data4['team_member_join_time'] = date('Y-m-d H:i:s');
                        Db::table('up_team_members')->where($where4)->insert($data4);
                    }else{//存在的话
                        if($role1['team_member_remove']==1){//先判断这个人已经被移出
                            $data5['team_member_remove'] = 2;
                            if($role1['team_member_role']!=2){
                                $data5['team_member_role'] =2;//管理层权限
                            }
                            $where5['team_member_id'] = $role1['team_member_id'];
                            Db::table('up_team_members')->where($where5)->update($data5);
                        }else{//没有被移出
                            if($role1['team_member_role']!=2){
                                $data6['team_member_role'] =2;//管理层权限
                                $where6['team_member_id'] = $role1['team_member_id'];
                                Db::table('up_team_members')->where($where6)->update($data6);
                            }
                        }

                    }
                }
                $msg1 = "{\"msg\":\"15\"}";
                $msg = '平台管理员把您设为'.$team_list['team_name'].'团队管理的管理层';
                $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                $code = 15;
                $code1 = 15;
                $this->sendJpush(1,$msg,$msg_data,$_POST['teamRole2'],$msg1,$code,1,$code1);

            }

        }
        if(isset($_POST['teamRole3'])===true){
            if(count($_POST['teamRole3'])>0){//团队成员
                foreach ($_POST['teamRole3'] as $kk=>$vv){
                    $where7['team_id'] = $team_id;
                    $where7['team_member_user_id'] =$vv;
                    $role1 = Db::table('up_team_members')->where($where7)->find();
                    if($role1['team_member_id']==''){//不存在直接插入
                        $data7['team_member_id'] = $this->getRandChar(32);
                        $data7['team_id'] = $team_id;
                        $data7['team_member_user_id'] = $vv;
                        $data7['team_member_role'] = 3;
                        $data7['team_member_join_time'] = date('Y-m-d H:i:s');
                        Db::table('up_team_members')->where($where7)->insert($data7);
                    }else{//存在的话
                        if($role1['team_member_remove']==1){//先判断这个人已经被移出
                            $data8['team_member_remove'] = 2;
                            if($role1['team_member_role']!=3){
                                $data8['team_member_role'] =3;//成员层
                            }
                            $where8['team_member_id'] = $role1['team_member_id'];
                            Db::table('up_team_members')->where($where8)->update($data8);
                        }else{//没有被移出
                            if($role1['team_member_role']!=2){
                                $data9['team_member_role'] =3;//管理层权限
                                $where9['team_member_id'] = $role1['team_member_id'];
                                Db::table('up_team_members')->where($where9)->update($data9);
                            }
                        }

                    }
                }
                $msg1 = "{\"msg\":\"15\"}";
                $msg = '平台管理员把您设为'.$team_list['team_name'].'团队管理的成员层';
                $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                $code = 15;
                $code1 = 15;
                $this->sendJpush(1,$msg,$msg_data,$_POST['teamRole3'],$msg1,$code,1,$code1);
            }

        }
        $returnData['status'] = 1;
        $returnData['msg'] = "团队成员添加成功";
        return json_encode($returnData);
    }

    /**
     * 编辑团队
     */
    public function updateTeamMember(){
        $nid =$_POST['nid'];
        $team_one = Db::table('up_team')->where('team_id',$nid)->find();
        //这个团队决策成员
        $where['team_id'] = $nid;
        $where['team_member_role'] =1;
        $where['team_member_remove'] = 2;
        $member1 = Db::table('up_team_members')->where($where)->column('team_member_user_id');
        //这个团队管理成员
        $where1['team_id'] = $nid;
        $where1['team_member_role'] =2;
        $where1['team_member_remove'] = 2;

        $member2 = Db::table('up_team_members')->where($where1)->column('team_member_user_id');
        //这个团队成员
        $where2['team_id'] = $nid;
        $where2['team_member_role'] =3;
        $where2['team_member_remove'] = 2;

        $member3 = Db::table('up_team_members')->where($where2)->column('team_member_user_id');
        $whereU['user_status'] = 1;
        $userInfo = Db::table('up_u_user')->where($whereU)->field('user_id,user_username,user_phone')->select();
        $this->assign('userInfo',$userInfo);
        $this->assign('team_one',$team_one);
        $this->assign('member1',$member1);
        $this->assign('member2',$member2);
        $this->assign('member3',$member3);
        return $this->fetch('team/updateTeamMember');
    }

    public function updateTeamMemberInfo(){
        //判断权限是否存在交集
        $intersection1='';
        $intersection2='';
        $intersection3='';
        if(isset($_POST['teamRole1'])&&isset($_POST['teamRole2'])){
            $intersection1 = array_intersect($_POST['teamRole1'],$_POST['teamRole2']);
        }
        if(isset($_POST['teamRole1'])&&isset($_POST['teamRole3'])){
            $intersection2 = array_intersect($_POST['teamRole1'],$_POST['teamRole3']);
        }
        if(isset($_POST['teamRole2'])&&isset($_POST['teamRole3'])){
            $intersection3 = array_intersect($_POST['teamRole2'],$_POST['teamRole3']);
        }

        if($intersection1||$intersection2||$intersection3){
            $returnData['status'] = 0;
            $returnData['msg'] = "添加团队成员不能出现一个人多个角色";
            return json_encode($returnData);
        }
        $team_id = $_POST['team_one'];//团队ID
        $team_list = Db::table('up_team')->where('team_id',$team_id)->field('team_name')->find();
        //团队决策层
        if(isset($_POST['teamRole1'])===true){
            if(count($_POST['teamRole1'])>0){//团队决策层
                foreach ($_POST['teamRole1'] as $key=>$vo){
                    $where1['team_id'] = $team_id;
                    $where1['team_member_user_id'] =$vo;
                    $role1 = Db::table('up_team_members')->where($where1)->find();
                    if($role1['team_member_id']==''){//不存在直接插入
                        $data1['team_member_id'] = $this->getRandChar(32);
                        $data1['team_id'] = $team_id;
                        $data1['team_member_user_id'] = $vo;
                        $data1['team_member_role'] = 1;
                        $data1['team_member_join_time'] = date('Y-m-d H:i:s');
                        Db::table('up_team_members')->where($where1)->insert($data1);
                        $msg1 = "{\"msg\":\"15\"}";
                        $msg = '平台管理员把您设为'.$team_list['team_name'].'团队管理的决策层';
                        $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                        $code = 15;
                        $code1 = 15;
                        $this->sendJpush(1,$msg,$msg_data,$vo,$msg1,$code,2,$code1);
                    }else{//存在的话
                        if($role1['team_member_remove']==1){//先判断这个人已经被移出
                            $data2['team_member_remove'] = 2;
                            if($role1['team_member_role']!=1){
                                $data2['team_member_role'] =1;
                            }
                            $where2['team_member_id'] = $role1['team_member_id'];
                            Db::table('up_team_members')->where($where2)->update($data2);
                            $msg1 = "{\"msg\":\"15\"}";
                            $msg = '平台管理员把您设为'.$team_list['team_name'].'团队管理的决策层';
                            $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                            $code = 15;
                            $code1 = 15;
                            $this->sendJpush(1,$msg,$msg_data,$vo,$msg1,$code,2,$code1);
                        }else{//没有被移出
                            if($role1['team_member_role']!=1){
                                $data3['team_member_role'] =1;
                                $data3['team_member_remove'] = 2;
                                $where3['team_member_id'] = $role1['team_member_id'];
                                Db::table('up_team_members')->where($where3)->update($data3);
                                $msg1 = "{\"msg\":\"15\"}";
                                $msg = '平台管理员把您设为'.$team_list['team_name'].'团队管理的决策层';
                                $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                                $code = 15;
                                $code1 = 15;
                                $this->sendJpush(1,$msg,$msg_data,$vo,$msg1,$code,2,$code1);
                            }
                        }
                    }
                }
                //把没有添加的决策层全部移出
                $where1_1_1['team_id'] = $team_id;
                $where1_1_1['team_member_user_id'] =array('not in',$_POST['teamRole1']);
                $where1_1_1['team_member_role'] =1;
                //新加的where条件
                $where1_1_1_1['team_member_remove'] =2;
                $where1_1_1_1['team_id'] = $team_id;
                $where1_1_1_1['team_member_user_id'] =array('not in',$_POST['teamRole1']);
                $where1_1_1_1['team_member_role'] =1;
                $team_member_user_id =Db::table('up_team_members')->where($where1_1_1_1)->column('team_member_user_id');
                if(count($team_member_user_id)>0){
                    $msg ='平台管理员把您从'.$team_list['team_name'].'中移出';
                    $this->JgTs($msg,$team_member_user_id,'','');
                }
                Db::table('up_team_members')->where($where1_1_1)->setField('team_member_remove',1);
            }

        }else{//没有的话全部移出
            $where1_3['team_id'] = $team_id;
            $where1_3['team_member_role'] =1;
            //新加的where条件
            $where1_3['team_member_remove'] =2;
            $team_member_user_id = Db::table('up_team_members')->where($where1_3)->column('team_member_user_id');//查询出有没有数据
            if(count($team_member_user_id)>0){
                $where1_1['team_id'] = $team_id;
                $where1_1['team_member_role'] =1;
                Db::table('up_team_members')->where($where1_1)->setField('team_member_remove',1);
                $msg1 = '平台管理员把您从'.$team_list['team_name'].'中移出';
                $this->JgTs($msg1,$team_member_user_id,'','');
            }
        }
//团队管理层
        if(isset($_POST['teamRole2'])===true){
            if(count($_POST['teamRole2'])>0){//团队管理层
                foreach ($_POST['teamRole2'] as $k=>$v){
                    $where4['team_id'] = $team_id;
                    $where4['team_member_user_id'] =$v;
                    $role1 = Db::table('up_team_members')->where($where4)->find();
                    if($role1['team_member_id']==''){//不存在直接插入
                        $data4['team_member_id'] = $this->getRandChar(32);
                        $data4['team_id'] = $team_id;
                        $data4['team_member_user_id'] = $v;

                        $data4['team_member_role'] = 2;
                        $data4['team_member_join_time'] = date('Y-m-d H:i:s');
                        Db::table('up_team_members')->where($where4)->insert($data4);
                        $msg1 = "{\"msg\":\"15\"}";
                        $msg='平台管理员把您设为'.$team_list['team_name'].'团队管理的管理层';
                        $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                        $code = 15;
                        $code1 = 15;
                        $this->sendJpush(1,$msg,$msg_data,$v,$msg1,$code,2,$code1);
                    }else{//存在的话
                        if($role1['team_member_remove']==1){//先判断这个人已经被移出
                            $data5['team_member_remove'] = 2;
                            if($role1['team_member_role']!=2){
                                $data5['team_member_role'] =2;//管理层权限
                            }
                            $where5['team_member_id'] = $role1['team_member_id'];
                            Db::table('up_team_members')->where($where5)->update($data5);
                            $msg1 = "{\"msg\":\"15\"}";
                            $msg='平台管理员把您设为'.$team_list['team_name'].'团队管理的管理层';
                            $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                            $code = 15;
                            $code1 = 15;
                            $this->sendJpush(1,$msg,$msg_data,$v,$msg1,$code,2,$code1);
                        }else{//没有被移出
                            if($role1['team_member_role']!=2){
                                $data6['team_member_role'] =2;//管理层权限
                                $data6['team_member_remove'] = 2;
                                $where6['team_member_id'] = $role1['team_member_id'];
                                Db::table('up_team_members')->where($where6)->update($data6);
                                $msg1 = "{\"msg\":\"15\"}";
                                $msg='平台管理员把您设为'.$team_list['team_name'].'团队管理的管理层';
                                $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                                $code = 15;
                                $code1 = 15;
                                $this->sendJpush(1,$msg,$msg_data,$v,$msg1,$code,2,$code1);
                            }
                        }
                    }
                }

                //把没有添加的决策层全部移出
                $where1_1_2['team_id'] = $team_id;
                $where1_1_2['team_member_user_id'] =array('not in',$_POST['teamRole2']);
                $where1_1_2['team_member_role'] =2;
                //新加的where条件
                $where1_1_2_2['team_member_remove'] =2;
                $where1_1_2_2['team_id'] = $team_id;
                $where1_1_2_2['team_member_user_id'] =array('not in',$_POST['teamRole2']);
                $where1_1_2_2['team_member_role'] =2;
                $team_member_user_id2 =Db::table('up_team_members')->where($where1_1_2_2)->column('team_member_user_id');
                if(count($team_member_user_id2)>0){
                    $msg1 = '平台管理员把您从'.$team_list['team_name'].'中移出';
                    $this->JgTs($msg1,$team_member_user_id2,'','');
                }
                Db::table('up_team_members')->where($where1_1_2)->setField('team_member_remove',1);

            }

        }else{
            //没有的话全部移出
            $where1_2_2['team_id'] = $team_id;
            $where1_2_2['team_member_role'] =2;
            //新加的where条件
            $where1_2_2['team_member_remove'] =2;
            $res1 = Db::table('up_team_members')->where($where1_2_2)->column('team_member_user_id');//查询出有没有数据
            if(count($res1)>0){
                $where1_1_3['team_id'] = $team_id;
                $where1_1_3['team_member_role'] =2;
                $msg1_1 = '平台管理员把您从'.$team_list['team_name'].'中移出';
                $this->JgTs($msg1_1,$res1,'','');
                Db::table('up_team_members')->where($where1_1_3)->setField('team_member_remove',1);
            }
        }

        /*成员**/
        if(isset($_POST['teamRole3'])===true){
            if(count($_POST['teamRole3'])>0){//团队成员
                foreach ($_POST['teamRole3'] as $kk=>$vv){
                    $where7['team_id'] = $team_id;
                    $where7['team_member_user_id'] =$vv;
                    $role1 = Db::table('up_team_members')->where($where7)->find();
                    if($role1['team_member_id']==''){//不存在直接插入
                        $data7['team_member_id'] = $this->getRandChar(32);
                        $data7['team_id'] = $team_id;
                        $data7['team_member_user_id'] = $vv;
                        $data7['team_member_role'] = 3;
                        $data7['team_member_join_time'] = date('Y-m-d H:i:s');
                        Db::table('up_team_members')->where($where7)->insert($data7);
                        $msg1 = "{\"msg\":\"15\"}";
                        $msg='平台管理员把您设为'.$team_list['team_name'].'团队管理的成员层';
                        $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                        $code = 15;
                        $code1 = 15;
                        $this->sendJpush(1,$msg,$msg_data,$vv,$msg1,$code,1,$code1);
                    }else{//存在的话
                        if($role1['team_member_remove']==1){//先判断这个人已经被移出
                            $data8['team_member_remove'] = 2;
                            if($role1['team_member_role']!=3){
                                $data8['team_member_role'] =3;//成员层
                            }
                            $where8['team_member_id'] = $role1['team_member_id'];
                            Db::table('up_team_members')->where($where8)->update($data8);
                            $msg1 = "{\"msg\":\"15\"}";
                            $msg='平台管理员把您设为'.$team_list['team_name'].'团队管理的成员层';
                            $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                            $code = 15;
                            $code1 = 15;
                            $this->sendJpush(1,$msg,$msg_data,$vv,$msg1,$code,2,$code1);
                        }else{//没有被移出
                            if($role1['team_member_role']!=3){
                                $data9['team_member_role'] =3;//管理层权限
                                $data9['team_member_remove'] = 2;
                                $where9['team_member_id'] = $role1['team_member_id'];
                                Db::table('up_team_members')->where($where9)->update($data9);
//                                $msg11 = "{\"msg\":\"15\"}";
//                                $msg6 = '平台管理员把您设为'.$team_list['team_name'].'团队管理的成员层';
//                                $msg_data6 =array('msg'=>$msg6,'code'=>15);
//                                $this->sendJpush(1,$msg6,$msg_data6,$vv,$msg11);
                                $msg1 = "{\"msg\":\"15\"}";
                                $msg='平台管理员把您设为'.$team_list['team_name'].'团队管理的成员层';
                                $msg_data = array('msg'=>$msg,'code'=>15,'fid'=>'');
                                $code = 15;
                                $code1 = 15;
                                $this->sendJpush(1,$msg,$msg_data,$vv,$msg1,$code,2,$code1);
                            }
                        }
                    }
                }
                //把没有添加的决策层全部移出
                $where1_1_4['team_id'] = $team_id;
                $where1_1_4['team_member_user_id'] =array('not in',$_POST['teamRole3']);
                $where1_1_4['team_member_role'] =3;
                $where1_1_4['team_member_remove'] =2;
                $team_member_user_id7 =Db::table('up_team_members')->where($where1_1_4)->column('team_member_user_id');

                $where1_1_4_4['team_id'] = $team_id;
                $where1_1_4_4['team_member_user_id'] =array('not in',$_POST['teamRole3']);
                $where1_1_4_4['team_member_role'] =3;
                if(count($team_member_user_id7)>0){
                    $msg1_1 = '平台管理员把您从'.$team_list['team_name'].'中移出';
                    $this->JgTs($msg1_1,$team_member_user_id7,'','');
                }
                Db::table('up_team_members')->where($where1_1_4_4)->setField('team_member_remove',1);
            }

        }else{
            //没有的话全部移出
            $where1_4_4['team_id'] = $team_id;
            $where1_4_4['team_member_role'] =3;
            $where1_4_4['team_member_remove'] =2;
            $res3 = Db::table('up_team_members')->where($where1_4_4)->column('team_member_user_id');//查询出有没有数据
            if(count($res3)>0){
                $where1_1_5['team_id'] = $team_id;
                $where1_1_5['team_member_role'] =3;
                $msg1_1 = '平台管理员把您从'.$team_list['team_name'].'中移出';
                $this->JgTs($msg1_1,$res3,'','');
                Db::table('up_team_members')->where($where1_1_5)->setField('team_member_remove',1);
            }
        }
        $returnData['status'] = 1;
        $returnData['msg'] = "团队成员编辑成功";
        return json_encode($returnData);
    }
}
